package com.example.review.Fragment;


import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;

import com.example.review.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * A simple {@link Fragment} subclass.
 */
public class CameraFragment extends Fragment {

    @BindView(R.id.xiangji)
    Button xiangji;
    @BindView(R.id.xiangjiimg)
    ImageView xiangjiimg;
    private View view;

    public CameraFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_camera, container, false);
        initView();
        return view;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    private void initView() {
        String[] qx = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.CAMERA};
        if (Build.VERSION.SDK_INT >= 23) {//6.0才用动态权限
            power(qx);
        }

    }
    private void power(String[] s){
        List<String> list = new ArrayList<>();
        for (int i=0;i<s.length;i++){
            int camera = ContextCompat.checkSelfPermission(getContext(), s[i]);
            if(camera!=PackageManager.PERMISSION_GRANTED){
                list.add(s[i]);
            }
        }
        if (list.size() > 0) {//有权限没有通过，需要申请
            ActivityCompat.requestPermissions(getActivity(), s, 100);
        }else{
            //说明权限都已经通过，可以做你想做的事情去
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        boolean hasPermissionDismiss = false;//有权限没有通过
        if (100 == requestCode) {
            for (int i = 0; i < grantResults.length; i++) {
                if (grantResults[i] == -1) {
                    hasPermissionDismiss = true;
                }
            }
            //如果有权限没有被允许
            if (hasPermissionDismiss) {
                showPermissionDialog();//跳转到系统设置权限页面，或者直接关闭页面，不让他继续访问
            }else{
                //全部权限通过，可以进行下一步操作。。。

            }
        }
    }

    private void showPermissionDialog() {
        Toast.makeText(getContext(),"已禁用权限，请手动授予",Toast.LENGTH_SHORT).show();
        Uri packageURI = Uri.parse("package:" + "com.example.review");
        Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, packageURI);
        startActivity(intent);

    }
//        if (mPermissionDialog == null) {
//            mPermissionDialog = new AlertDialog.Builder(this)
//                    .setMessage("已禁用权限，请手动授予")
//                    .setPositiveButton("设置", new DialogInterface.OnClickListener() {
//                        @Override
//                        public void onClick(DialogInterface dialog, int which) {
//                            cancelPermissionDialog();
//
//                            Uri packageURI = Uri.parse("package:" + mPackName);
//                            Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, packageURI);
//                            startActivity(intent);
//                        }
//                    })
//                    .setNegativeButton("取消", new DialogInterface.OnClickListener() {
//                        @Override
//                        public void onClick(DialogInterface dialog, int which) {
//                            //关闭页面或者做其他操作
//                            cancelPermissionDialog();
//
//                        }
//                    })
//                    .create();
//        }
//        mPermissionDialog.show();
//    }
//
//    //关闭对话框
//    private void cancelPermissionDialog() {
//        mPermissionDialog.cancel();
//    }
}
